<template>
	<Modal
	  title="添加"
	  :value="value"
	  :mask-closable="true"
	  :footer-hide="true"
	  @input="$emit('input', $event)"
	>
	  <Form
	    ref="formLkyAssessment"
	    :model="formLkyAssessment"
	    :rules="ruleValidate"
	    label-position="right"
	    :label-width="80"
	  >
	    <FormItem label="实训项目" prop="training_project">
	      <Input type="text" v-model="formLkyAssessment.training_project" placeholder="实训项目"/>
	    </FormItem>
	    <FormItem label="实训开始时间" prop="start_time">
        <DatePicker v-model="formLkyAssessment.start_time" type="date" format="yyyy/MM/dd" placeholder="实训开始时间" style="width: 200px" ></DatePicker>
<!--	      <Input type="date" v-model="formLkyAssessment.start_time" placeholder=""/>-->
	    </FormItem>
		<FormItem label="实训结束时间" prop="end_time">
      <DatePicker v-model="formLkyAssessment.end_time" type="date" format="yyyy/MM/dd" placeholder="实训结束时间" style="width: 200px" ></DatePicker>
<!--		  <Input type="date" v-model="formLkyAssessment.end_time" placeholder="实训结束时间"/>-->
		</FormItem>
	    <FormItem label="实训地点" prop="training_site">
	      <Input type="text" v-model="formLkyAssessment.training_site" placeholder="实训地点"/>
	    </FormItem>
			<FormItem label="指导老师" prop="guidance_teacher">
			  <Input type="text" v-model="formLkyAssessment.guidance_teacher" placeholder="指导老师"/>
			</FormItem>
			<FormItem label="分值" prop="score">
			  <Input number v-model="formLkyAssessment.score" placeholder="分值"/>
			</FormItem>
	    <FormItem>
	      <Button type="primary" @click="handleSubmit('formLkyAssessment')">提交</Button>
	      <Button @click="handleReset('formLkyAssessment')" style="margin-left: 8px">重置</Button>
	    </FormItem>
	  </Form>
	</Modal>
</template>

<script>
import { save } from '@/api/lky/lkyAssessment'
import { createModelObj, getToken } from '@/libs/util'

export default {
	  name: 'Add',
	  props: {
	    value: {
	      type: Boolean,
	      default: false
	    },
    LkyAssessment: {
      type: Object,
      default: null
    }
	  },
	  data () {
	    return {
	      formLkyAssessment: {
			 tid: '',
			 sid: '',
	        training_project: '',
	        start_time: '',
        end_time: '',
	        training_site: '',
	        guidance_teacher: '',
        score: ''
	      },
	      ruleValidate: {

	        training_project: [
	          { required: true, message: '该值不能为空', trigger: 'blur' },
	          { type: 'string', max: 20, message: '长度最长为20', trigger: 'blur' }
	        ],

	        start_time: [
          { required: true, message: '该值不能为空', trigger: 'blur', type: 'date' }
	        ],
        end_time: [
          { required: true, message: '该值不能为空', trigger: 'blur', type: 'date' }
	        ],
	        training_site: [
	          { required: true, message: '该值不能为空', trigger: 'blur' },
	          { type: 'string', max: 200, message: '长度最长为200', trigger: 'blur' }
	        ],
        guidance_teacher: [
					  { required: true, message: '该值不能为空', trigger: 'blur' },
					  { type: 'string', max: 200, message: '长度最长为200', trigger: 'blur' }
        ],
        score: [
          {
            required: true,
            type: 'number',
            message: '请输入数字',
            trigger: 'blur'
          },
          { type: 'number', max: 120, message: '最大值120', trigger: 'blur' },
          { type: 'number', min: 0, message: '最小值0', trigger: 'blur' }
        ]
	      }
	    }
	  },
	  methods: {
	    handleSubmit (name) {
	      this.$refs[name].validate(valid => {
	        if (valid) {
	          save(Object.assign({}, createModelObj(this.formLkyAssessment, 'lkyEvaluation'))).then(
	            res => {
	              if (res) {
	                this.$emit('addRow', res.row)
	                this.$emit('input', false)
	                this.handleReset(name)
	              }
	            }
	          )
	        } else {
	          this.$Message.error('验证失败!')
	        }
	      })
	    },
	    handleReset (name) {
	      this.$refs[name].resetFields()
	    }
	  },
	  watch: {
		  value: function (val, oldVal) {
			  this.$refs.formLkyAssessment.resetFields()
			  this.formLkyAssessment.tid = getToken()
			  this.formLkyAssessment.sid = this.LkyAssessment.ID
			  coverFormProps(this.formLkyAssessment, this.lkyAssessment)
		  }
		  },
		  LkyAssessment (n) {
			  if (n != null) {
				  this.formLkyAssessment = n
				  this.sid = n.id
		  }
  }
}
</script>
